package com.wx.spider.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author:feng
 * @create:2021-04-26 12:41
 */
public class HtmlUtil {
    private static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
    private static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String REGEX_HTML = "<[^>]+>"; // 定义HTML标签的正则表达式
    //private static final String REGEX_SPACE = "\\s*|\t|\r|\n";//定义空格回车换行符
    private static final String REGEX_SPACE = "\t|\r|\n";//定义空格回车换行符

    /**
     * @param htmlStr
     * @return
     *  删除Html标签
     */
    public static String delHTMLTag(String htmlStr) {
        if (htmlStr == null || "".equals(htmlStr.trim())){
            return "";
        }
        Pattern p_script = Pattern.compile(REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(REGEX_STYLE, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        htmlStr=htmlStr.replaceAll("<p>", "\r\n");
        htmlStr=htmlStr.replaceAll("<br/>", "\r\n");
        htmlStr=htmlStr.replaceAll("<br>", "\r\n");
        htmlStr=htmlStr.replaceAll("<p/>", "\r\n");

        Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签
        htmlStr=htmlStr.replaceAll("[ ]+", " ");
        htmlStr=htmlStr.replaceAll("[(\r\n )]+", "\r\n");
        htmlStr=htmlStr.replaceAll("[(\r\n)]+", "\r\n");

        //Pattern p_space = Pattern.compile(REGEX_SPACE, Pattern.CASE_INSENSITIVE);
        //Matcher m_space = p_space.matcher(htmlStr);
        //htmlStr = m_space.replaceAll("").replaceAll("<br>", "").replaceAll("<br >", ""); // 过滤空格回车标签
        return htmlStr.trim(); // 返回文本字符串
    }

    public static String getTextFromHtml(String htmlStr){
        htmlStr = delHTMLTag(htmlStr);
        return htmlStr;
    }
    //不删除段落标签
    public static String getTextFromHtml1(String htmlStr){
        Pattern p_script = Pattern.compile(REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(REGEX_STYLE, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        htmlStr=htmlStr.replaceAll("<p>", "##P_START##");
        htmlStr=htmlStr.replaceAll("</p>", "##P_END##");

        Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        htmlStr=htmlStr.replaceAll("##P_START##", "<p>");
        htmlStr=htmlStr.replaceAll("##P_END##", "</p>");
        return htmlStr.trim(); // 返回文本字符串
    }

    public static void main(String[] args) {
        String s = "<!DOCTYPE html PUBLIC ''-//WAPFORUM//DTD XHTML Mobile 1.0//EN'' ''http://www.wapforum.org/DTD/xhtml-mobile10.dtd''><html xmlns=''http://www.w3.org/1999/xhtml''><head><meta http-equiv=''Content-Type'' content=''text/html; charset=utf-8'' /><meta id=''viewport'' name=''viewport'' content=''width=320,initial-scale=1,maximum-scale=1,user-scalable=0;'' /><title>概述-白塞病诊断和治疗指南</title><link href=''Pub/css/layout.css'' rel=''stylesheet'' type=''text/css'' /><link href=''Pub/css/public.css'' rel=''stylesheet'' type=''text/css'' /><script type=''text/javascript'' src=''Pub/js/jquery-1.6.1.min.js''></script><script type=''text/javascript'' src=''Pub/js/base.js''></script></head><body><div id=''Layout''><div class=''titleBar''><h1 id= ''1''>1. 概述</h1></div><div class=''mainContainer''><p>白塞病(Behcet's disease，BD)又称贝赫切特病、口-眼-生殖器三联征等。 </p></div></div></body></html>";
        System.out.println(getTextFromHtml(s));
    }


    //提取src的值
    public static List<String> getImgSrc(String content){

        List<String> list = new ArrayList<String>();
        //
        Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
        //img匹配器
        Matcher m_img = p_img.matcher(content);

        boolean result_img = m_img.find();
        if (result_img) {
            while (result_img) {
                //获取到img标签中的内容
                String str_img = m_img.group(2);
                //
                Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                //src匹配器
                Matcher m_src = p_src.matcher(str_img);
                if (m_src.find()) {

                    //获取到匹配的src中的内容
                    String str_src = m_src.group(3);
                    list.add(str_src);
                }

                //匹配content中是否存在下一个<img />标签，有则继续以上步骤匹配<img />标签中的src
                result_img = m_img.find();
            }
        }
        return list;
    }

}